<template>
    <div >
      <zh-row>
        <zh-col :span="4">
          <file-class-tree ref="classTreeRef" @nodeClick="treeNodeClick" style="height:800px">
          </file-class-tree>
        </zh-col>
        <zh-col :span="20">
          <zh-card title="">
            <zh-form :model="state.queryParams" ref="queryFormRef" label-width="150px" :rules="state.rules">
              <zh-form-select
                  prop="appId"
                  :lazy="true"
                  label="我的应用"
                  v-model="state.queryParams.appId"
                  :fieldNames="{ label: 'appName', value: 'appId' }"
                  funcCode="application0012"
                  searchField="appName"
              ></zh-form-select>
              <zh-form-select
                  prop="upAppId"
                  :lazy="true"
                  label="所属应用"
                  v-model="state.queryParams.upAppId"
                  :fieldNames="{ label: 'appName', value: 'appId' }"
                  funcCode="application0012"
                  searchField="appName"
              ></zh-form-select>
              <zh-form-select
                  prop="secrecyLevel"
                  label="保密级别"
                  :options="state.secrecyLevelDict"
                  v-model="state.queryParams.secrecyLevel"
              ></zh-form-select>
              <zh-form-select
                  prop="fieldId"
                  :lazy="true"
                  label="元数据"
                  v-model="state.queryParams.fieldId"
                  :fieldNames="{ label: 'fieldName', value: 'fieldId' }"
                  :url="VITE_FILE_API_URL+'fileFieldParam0001'"                 
                  searchField="fieldName"
              ></zh-form-select>
              <zh-form-input prop="fieldValue" label="文件元数据值"  v-model="state.queryParams.fieldValue"
                            ></zh-form-input>
              <zh-form-query v-model="state.advanced"  :form-ref="queryFormRef" @queryClick="clearData"
                             :table-ref="fileTable">
                            </zh-form-query>
            </zh-form>
          </zh-card>
          <zh-table
              ref="fileTable"
              title="文件列表"
              :height="600"
              :url="VITE_FILE_API_URL+'fileManager0010'"
              :columns="state.tableColumn"
              :disabled="false"
              highlight-current-row
          >
            <template #secrecyLevel="{row}">
              <zh-select
                  :disabled="true"
                  prop="secrecyLevel"
                  label="保密级别"
                  :options="state.secrecyLevelDict"
                  v-model="row.secrecyLevel"
              ></zh-select>
            </template>
            <template #encryptionFlag="{row}">
              <zh-select
                  :disabled="true"
                  prop="encryptionFlag"
                  label="是否加密"
                  :options="state.dictData.flag"
                  v-model="row.encryptionFlag"
              ></zh-select>
            </template>
            <template #fielderFlag="{row}">
              <zh-select
                  :disabled="true"
                  prop="fielderFlag"
                  label="是否归档"
                  :options="state.dictData.flag"
                  v-model="row.fielderFlag"
              ></zh-select>
            </template>
            <template #op="{row}">
              <el-tooltip content="元数据" placement="top">
                <el-button :icon="Link" link @click="link(row)"></el-button>
              </el-tooltip>
              <el-tooltip content="编辑" placement="top">
                <el-button :icon="Edit" link @click="edit(row)"></el-button>
              </el-tooltip>
              <el-tooltip content="下载" placement="top">
                <el-button :icon="Download" link @click="download(row)"></el-button>
              </el-tooltip>
            </template>
          </zh-table>
        </zh-col>
      </zh-row>
      <el-dialog
          ref="fileInfoDialog"
          v-model="state.fileInfoVisible"
          title="文件信息"
          width="80%">
          <file-info ref="fileInfoRef" :disabled="true"></file-info>
      </el-dialog>
      <el-dialog
          ref="fileMetaDataDialog"
          v-model="state.fileMetaDataVisible"
          title="文件元数据"
          width="80%"
        >
          <file-meta-data ref="fileMetaDataRef" disabled="true"></file-meta-data>
      </el-dialog>

      <zh-dialog
          ref="fileCollectTreeDialog"
          v-model="state.fileCollectTreeVisible"
          title="文件分类树"
          width="40%"
          @confirm="saveFileCollectTree">
          <FileCollectTree ref="fileCollectTreeRef"></FileCollectTree>
      </zh-dialog>
    </div>
</template>

<script lang="ts" setup name="fileManagerIndex">
import {Edit,Link,Download} from '@element-plus/icons-vue'
import FileClassTree from '@/views/fileCenter/fileClass/component/FileClassTree.vue';
import FileInfo from '@/views/fileCenter/fileManager/component/fileInfo.vue';
import FileMetaData from '@/views/fileCenter/fileManager/component/fileMetaData.vue';
import FileCollectTree from '@/views/fileCenter/fileCollectClass/component/FileCollectTree.vue';
import { ElMessageBox } from "element-plus";
const VITE_FILE_API_URL = import.meta.env.VITE_FILE_API_URL as string+'/doc/api/';


//-----------------定义组件--------------
const queryFormRef = ref<any>({});
const fileTable = ref<any>({});
const classTreeRef=ref();
const fileInfoRef=ref();
const fileMetaDataRef=ref();
const fileCollectTreeDialog=ref();
const fileCollectTreeRef=ref();

const state = reactive({
  rules:{
    appId: [
      {
        required: true,
        message: '请选择所属应用',
        trigger: 'blur',
      },
    ],

  },
  fileMetaDataVisible:false,
  fileInfoVisible:false,
  fileCollectTreeVisible:false,
  dictData:[],
  advanced:true,
  queryParams:{
    fileClassCode:''
  },
  secrecyLevelDict:[
    {
      dictKey:'1',
      dictValue:'一级'
    },
    {
      dictKey:'2',
      dictValue:'二级'
    },
    {
      dictKey:'3',
      dictValue:'三级'
    },
  ],
  tableColumn:[
    {type: 'checkbox', width: 50, fixed: 'left' },
    {type: 'seq', width: 80, fixed: 'left', align: 'center', title: '序号'},
    {field: 'fileId', title: '文件id', fixed: 'left', visible: false, align: 'center', width: 150},
    {field: 'fileClassCode', title: '文件分类',visible: false, fixed: 'left', align: 'center', width: 150},
    {field: 'classTitle', title: '文件分类', fixed: 'left', align: 'center', width: 150},
    {field: 'fileName', title: '文件名称',  fixed: 'left',align: 'center', minWidth: 200},
    {field: 'showId', title: '文件显示id',  align: 'center', minWidth: 150},
    {field: 'fileSuffix', title: '文件后缀（文件类型）',  align: 'center', minWidth: 200},
    {field: 'fileEnvId', title: '文件环境主键',visible:false,  align: 'center', minWidth: 200},
    {field: 'filePath', title: '文件存放路径',  align: 'center', minWidth: 200},
    {field: 'fileSize', title: '文件大小',  align: 'center', minWidth: 200},
    {field: 'secrecyLevel', title: '保密级别',  align: 'center', minWidth: 200,slots: {default: 'secrecyLevel'}},
    {field: 'encryptionFlag', title: '是否加密',  align: 'center', minWidth: 200,slots: {default: 'encryptionFlag'}},
    {field: 'filePassowrd', title: '文件加密密码',  align: 'center', minWidth: 200},
    {field: 'zipPassword', title: '压缩包解压密码',  align: 'center', minWidth: 200},
    {field: 'fielderFlag', title: '是否归档',  align: 'center', minWidth: 200,slots: {default: 'fielderFlag'}},
    {field: 'upAppName', title: '上传应用',  align: 'center', minWidth: 200},
    {field: 'uploader', title: '上传人',  align: 'center', minWidth: 200},
    {field: 'uploaderId', title: '上传人id', visible:false, align: 'center', minWidth: 200},
    {field: 'fileder', title: '归档人',  align: 'center', minWidth: 200},
    {field: 'filederId', title: '归档人id', visible:false, align: 'center', minWidth: 200},
    {field: 'filederTime', title: '归档时间',  align: 'center', minWidth: 200},
    {field: 'remarks', title: '备注',  align: 'center', minWidth: 200},
    {field: 'fileMd5', title: '文件md5',  align: 'center', minWidth: 200},
    {field: 'fileClassPath', title: '文件存放路径',  align: 'center', minWidth: 200},
    // {field: 'envName', title: '环境名称',  align: 'center', minWidth: 200},
    {field: 'op', title: '操作',  align: 'center',fixed:'right',width:200,slots: {default: 'op'}},
  ]
});

//---------------------初始化---------------------
const params = {
  dictFields: 'flag',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;
});


//--------------------------定义调用方法--------------------------------------------------
//查询文件元数据信息
function link(row){
  let info= {fileId:row.fileId};
  state.fileMetaDataVisible=true;
  nextTick(()=>{
    fileMetaDataRef.value.setInitData(info);
  })
}

//查询文件详细信息
function edit(row){
  state.fileInfoVisible=true;
  nextTick(() => {
    fileInfoRef.value.setInitData(row);
  });
}

//下载文件
function download(row){
  let showId=row.showId;
  //拼接文件地址
  let path = $api.common.fileDownloadUrl.url + showId;
  let a = document.createElement('a');
  a.href = path;
  a.click();
}


//重置查询条件
function clearData(){

}
//获取当前节点赋值查询条件
function treeNodeClick(){
  state.queryParams.fileClassCode=classTreeRef.value.selectNode.key;
}

//归集弹窗文件树
function collectSave(){
  state.fileCollectTreeVisible=true;
}

//归集保存
function saveFileCollectTree(){
  let fileCollectCode = fileCollectTreeRef.value.fileCollectInfo.fileCollectCode;
  console.log('树ID：'+fileCollectTreeRef.value.fileCollectInfo.fileCollectCode);

  const checkedList = fileTable.value.getAllCheckboxRecords();
	if (checkedList) {
		if (checkedList.length <= 0) {
			$messageBox.warning('请先勾选需要归集的数据', '提示');
			return false;
		}
	} else {
		$messageBox.warning('请先勾选需要归集的数据', '提示');
		return false;
	}
	let fileIds: String = [];
	checkedList.forEach((item: any) => {
    console.log('id:'+item.fileId)
		fileIds.push(item.fileId);
	});

  let params={fileIds:fileIds,fileCollectCode:fileCollectCode};
  
  $api.common.fileSend.post('fileCollectData0007', params).then(res => {
    console.log('code:'+res.code);
      if (res.code == 0) {
          ElMessageBox.alert('归集成功');
          state.fileCollectTreeVisible=false;
      }
  })
  


  

}

</script>
